package ch.fusun.baron.weather;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import ch.fusun.baron.swt.isometry.components.IsometryWidget;
import ch.fusun.baron.swt.isometry.components.Sprite;

/**
 * Test class
 */
public class Starter {

	private static final int ALTITUDE = 15;
	private static final int SIZE = 20;

	private static int step = 0;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Automaton weather = new Automaton();
		weather.setWorld(new World(SIZE, SIZE, ALTITUDE));
		weather.put(new HeatDissipationUpdater(ALTITUDE));
		weather.put(new WaterFallingDownUpdater(ALTITUDE));
		weather.put(new VaporPressureCurveUpdater());
		weather.put(new VaporUpdater(ALTITUDE));

		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());

		final IsometryWidget widget = new IsometryWidget(shell, SWT.NONE, SIZE,
				SIZE, ALTITUDE, 32, 32, 16);
		widget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		shell.setText("Map"); //$NON-NLS-1$

		final Image cloud = new Image(display, "icons/cloud_alt.png"); //$NON-NLS-1$
		final Image rain = new Image(display, "icons/rain_alt.png"); //$NON-NLS-1$
		final Image ground = new Image(display, "icons/0_alt.png"); //$NON-NLS-1$
		// final Image mountain = new Image(display, "icons/15.png");
		// final Image sun = new Image(display, "icons/sun.png");
		final Image water = new Image(display, "icons/water_alt.png"); //$NON-NLS-1$

		for (int i = 0; i < SIZE; i++) {
			for (int j = 0; j < SIZE; j++) {
				for (int k = 0; k < ALTITUDE; k++) {
					weather.getWorld().getCube(i, j, k).setTemperature(278);
				}
			}
		}

		weather.getWorld().getCube(SIZE / 2, SIZE / 2, ALTITUDE / 2)
				.setWater(1000000);
		weather.getWorld().getCube(SIZE / 2, SIZE / 2, ALTITUDE / 2)
				.setVapour(5000);

		Button button = new Button(shell, SWT.PUSH);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				widget.clear();
				float total = 0;
				float totalWater = 0;
				float totalVapour = 0;
				float max = Float.MIN_VALUE;
				float min = Float.MAX_VALUE;
				String columnHeat = ""; //$NON-NLS-1$
				String columnWater = ""; //$NON-NLS-1$
				String columnVapour = ""; //$NON-NLS-1$
				step++;
				for (int i = 0; i < SIZE; i++) {
					weather.getWorld()
							.getCube(i, step % SIZE, 0)
							.setTemperature(
									weather.getWorld()
											.getCube(i, step % SIZE, 0)
											.getTemperature() + 280);
					for (int j = 0; j < SIZE; j++) {
						columnHeat = ""; //$NON-NLS-1$
						columnWater = ""; //$NON-NLS-1$
						columnVapour = ""; //$NON-NLS-1$
						for (int k = 0; k < ALTITUDE; k++) {
							Cube cube = weather.getWorld().getCube(i, j, k);
							columnHeat = columnHeat + " " //$NON-NLS-1$
									+ cube.getTemperature();
							columnWater = columnWater + " " + cube.getWater(); //$NON-NLS-1$
							columnVapour = columnVapour + " " //$NON-NLS-1$
									+ cube.getVapour();
							total += cube.getTemperature();
							totalWater += cube.getWater();
							totalVapour += cube.getVapour();
							if (max < cube.getTemperature()) {
								max = cube.getTemperature();
							}
							if (min > cube.getTemperature()) {
								min = cube.getTemperature();
							}
							if (cube.getZ() == 0) {
								widget.addItem(null, i, j, k, new Sprite(
										ground, null));
							}
							if (cube.getWater() > 0) {
								if (cube.getZ() == 0) {

									widget.addItem(null, i, j, k, new Sprite(
											water, null));
								} else {
									if (cube.getWater() < 20) {
										widget.addItem(null, i, j, k,
												new Sprite(cloud, null));
									} else {
										widget.addItem(null, i, j, k,
												new Sprite(rain, null));
									}
								}
							}
						}
					}
				}
				System.out.println("totheat: " + total + " totwat: " //$NON-NLS-1$ //$NON-NLS-2$
						+ totalWater + " totvapour: " + totalVapour //$NON-NLS-1$
						+ "\n max: " + max + " min: " + min + "\n TotWATPOR: " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
						+ (totalWater + totalVapour));
				System.out.println("heat: " + columnHeat); //$NON-NLS-1$
				System.out.println("water: " + columnWater); //$NON-NLS-1$
				System.out.println("vapour: " + columnVapour); //$NON-NLS-1$
				widget.redraw();
				weather.step();
			}
		});

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}

		}
		display.dispose();
	}
}
